package com.example.websocket.manager;

import com.example.websocket.model.Message;
import com.example.websocket.model.MessageData;
import com.example.websocket.model.WebsocketProperty;
import com.example.websocket.model.table.User;
import com.example.websocket.service.UserService;
import com.example.websocket.utils.JacksonUtils;
import com.example.websocket.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@ServerEndpoint("/websocket/{room}/{userId}")
@Component
public class WebSocketServer {

    /**
     * 静态变量，用来记录当前在线连接数。应该把它设计成线程安全的
     */
    private static volatile int onlineCount = 0;
    /**
     * concurrent包的线程安全Map，用来存放每个客户端对应的MyWebSocket对象。
     */
    private static ConcurrentHashMap<String, ConcurrentHashMap<String, WebSocketServer>> roomUserIdWebSocketMap = new ConcurrentHashMap<>();
    /**
     * 与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    private Session session;
    /**
     * 接收userId*
     */
    private String userId = "";
    private UserService userService = SpringUtils.getBean("userService");
    private WebsocketProperty websocketProperty = SpringUtils.getBean("websocketProperty");

    /**
     * 连接建立成功调用的方法
     * @param session
     * @param userId
     * @param room
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId, @PathParam("room") String room) {
        this.session = session;
        this.userId = userId;
        ConcurrentHashMap<String, WebSocketServer> userIdWebSocketMap = null;
        if (roomUserIdWebSocketMap.containsKey(room)) {
            String keyRoom = "";
            for (Map.Entry<String, ConcurrentHashMap<String, WebSocketServer>> entry : roomUserIdWebSocketMap.entrySet()) {
                keyRoom = entry.getKey();
                if (StringUtils.isNotBlank(keyRoom) && keyRoom.equals(room)) {
                    userIdWebSocketMap = entry.getValue();
                    if (!CollectionUtils.isEmpty(userIdWebSocketMap)) {
                        if (userIdWebSocketMap.containsKey(userId)) {
                            //重新连接
                            userIdWebSocketMap.remove(userId);
                            //加入Map中
                            userIdWebSocketMap.put(userId, this);
                        } else {
                            //加入Map中
                            userIdWebSocketMap.put(userId, this);
                            //在线数加1
                            addOnlineCount();
                        }
                    }
                    //roomUserIdWebSocketMap.remove(room);
                    roomUserIdWebSocketMap.put(room,userIdWebSocketMap);
                }
            }
        } else {
            userIdWebSocketMap = new ConcurrentHashMap<>();
            //加入Map中
            userIdWebSocketMap.put(userId, this);
            //在线数加1
            addOnlineCount();
            roomUserIdWebSocketMap.put(room, userIdWebSocketMap);
        }
        log.info(">>>用户连接userId:" + userId + ",当前在线人数为:" + getOnlineCount());
        try {
            this.message(room, userId,"open", new HashMap<>());
        } catch (IOException e) {
            log.error(">>>用户userId:" + userId + ",网络异常!!!!!!");
        }
    }


    /**
     *
     * @param room
     * @param userId
     * @param type
     * @param msgMap
     * @throws IOException
     */
    private void message(String room,String userId,String type,Map<String,Object> msgMap) throws IOException {
        if (CollectionUtils.isEmpty(msgMap)) {
            msgMap = new HashMap<>();
        }
        User findUser = userService.findUser(new User(userId));
        msgMap.put("userName", findUser.getName());
        msgMap.put("imgUrl", findUser.getImgUrl());
        if ("open".equals(type)) {
            msgMap.put("content", "欢迎用户[" + findUser.getName() + "]进入！");
        }
        if ("close".equals(type)) {
            msgMap.put("content", "用户[" + findUser.getName() + "]已退出！");
        }
        String toUserId = "";
        if (msgMap.containsKey("toUserId")) {
            toUserId = msgMap.get("toUserId").toString();
        }
        Message message = new Message(userId,toUserId, new MessageData(type,room,msgMap,HttpStatus.OK.value()),"webSocket连接成功");
        if (!CollectionUtils.isEmpty(roomUserIdWebSocketMap)) {
            for (Map.Entry<String, ConcurrentHashMap<String, WebSocketServer>> entry : roomUserIdWebSocketMap.entrySet()) {
                if (StringUtils.isNotBlank(entry.getKey()) && entry.getKey().equals(room)
                        && !CollectionUtils.isEmpty(entry.getValue())) {
                    if ("open".equals(type)) {
                        if (websocketProperty.isAcceptRoom()) {
                            for (Map.Entry<String, WebSocketServer> entry2 : entry.getValue().entrySet()) {
                                entry2.getValue().sendMessage(JacksonUtils.beanToJson(message));
                            }
                        } else {
                            entry.getValue().get(userId).sendMessage(JacksonUtils.beanToJson(message));
                        }
                    } else if ("send".equals(type)) {
                        if (StringUtils.isNotBlank(toUserId) && entry.getValue().containsKey(toUserId)) {
                            if (websocketProperty.isAcceptRoom()) {
                                for (Map.Entry<String, WebSocketServer> entry2 : entry.getValue().entrySet()) {
                                    entry2.getValue().sendMessage(JacksonUtils.beanToJson(message));
                                }
                            } else {
                                entry.getValue().get(toUserId).sendMessage(JacksonUtils.beanToJson(message));
                            }
                        } else {
                            // todo 否则不在这个服务器上，发送到mysql或者redis
                            if (websocketProperty.isAcceptRoom()) {
                                for (Map.Entry<String, WebSocketServer> entry2 : entry.getValue().entrySet()) {
                                    entry2.getValue().sendMessage(JacksonUtils.beanToJson(message));
                                }
                            }
                        }
                    } else if ("close".equals(type)) {
                        if (entry.getValue().containsKey(userId)) {
                            if (websocketProperty.isAcceptRoom()) {
                                for (Map.Entry<String, WebSocketServer> entry2 : entry.getValue().entrySet()) {
                                    if (!entry2.getKey().equals(userId)) {
                                        entry2.getValue().sendMessage(JacksonUtils.beanToJson(message));
                                    }
                                }
                            }
                            //从Map中删除
                            entry.getValue().remove(userId);
                            if (CollectionUtils.isEmpty(entry.getValue())) {
                                roomUserIdWebSocketMap.clear();
                            }
                            subOnlineCount();
                        }
                    } else {
                        log.error(">>>未知消息类型");
                    }
                }
            }
        }
    }


    /**
     * 收到客户端消息后调用的方法
     *
     *@param message 客户端发送过来的消息
     * @param session
     */
    @OnMessage
    public void onMessage(@PathParam("userId") String userId, @PathParam("room") String room, String message, Session session) {
        log.info(">>>发送消息,userId:" + userId + ",报文:" + message);
        if (StringUtils.isNotBlank(message)) {
            try {
                Optional<Map<String, Object>> optionalMap = JacksonUtils.str2Map(message, String.class, Object.class);
                Map<String, Object> msgMap = optionalMap.isPresent() ? optionalMap.get() : new HashMap<>();
                msgMap.put("fromUserId", this.userId);
                String toUserId = "";
                if (msgMap.containsKey("toUserId")) {
                    toUserId = msgMap.get("toUserId").toString();
                }
                if (StringUtils.isNotBlank(this.userId)) {
                    if (StringUtils.isNotBlank(toUserId)) {
                        if (this.userId.equals(toUserId) && !websocketProperty.isAcceptMyselft()) {
                            //不能给自己发送消息
                            log.warn(">>>不能给自己发送消息,fromUserId:{},toUserId:{}", this.userId, toUserId);
                            return;
                        }
                    }
                    //传送给对应toUserId用户的websocket
                    this.message(room, userId, "send", msgMap);
                }
            } catch (Exception e) {
                log.error(">>>发送消息异常", e);
            }
        }
    }


    /**
     * 连接关闭调用的方法
     * @param userId
     * @param room
     * @throws IOException
     */
    @OnClose
    public void onClose(@PathParam("userId") String userId, @PathParam("room") String room) throws IOException {
        this.message(room, userId, "close", new HashMap<>());
        log.info(">>>用户退出,userId:" + userId + ",当前在线人数为:" + getOnlineCount());
    }


    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error(">>>发生错误:" + this.userId + ",原因:" + error.getMessage());
    }


    /**
     * 实现服务器主动推送
     * @param message
     * @throws IOException
     */
    public void sendMessage(String message) throws IOException {
        if (session.isOpen()){
            this.session.getBasicRemote().sendText(message);
        }
    }


    /**
     * 发送自定义消息
     * @param message
     * @param toUserId
     * @throws IOException
     */
    public static void sendInfo(String message, @PathParam("userId") String toUserId, @PathParam("room") String room) throws IOException {
        log.info(">>>发送消息到userId:" + toUserId + "，报文:" + message);
        if (!CollectionUtils.isEmpty(roomUserIdWebSocketMap)) {
            for (Map.Entry<String, ConcurrentHashMap<String, WebSocketServer>> entry : roomUserIdWebSocketMap.entrySet()) {
                if (StringUtils.isNotBlank(entry.getKey()) && entry.getKey().equals(room)
                        && !CollectionUtils.isEmpty(entry.getValue())) {
                    if (StringUtils.isNotBlank(toUserId) && entry.getValue().containsKey(toUserId)) {
                        entry.getValue().get(toUserId).sendMessage(message);
                    } else {
                        log.error(">>>用户userId[" + toUserId + "],不在线！");
                    }
                }
            }
        }
    }


    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }
}
